﻿using Newtonsoft.Json.Linq;
using System.ComponentModel.DataAnnotations;

namespace YAPACS.RisServer.Services.ExtendCells;

/// <summary>
/// 文本单元格
/// </summary>
public class TextCell : Cell
{
    private string _text;

    public TextCell(string text, double width, int rowSpan, string textAlign)
    {
        originalWidth = width;
        RowSpan = rowSpan;
        Text = text;

        WidthCoculate();
        HeightCoculate();
        Style ??= [];
        Style.Add("position", "static");
        Style.Add("overflowX", "visible");
        Style.Add("overflowY", "visible");
        Style.Add("display", "flex");
        Style.Add("flex", "0 0 " + width);
        Style.Add("flexWrap", "nowrap");
        Style.Add("flexDirection", "row");
        SetTextAlign(textAlign);  // 对齐方式
    }
    public string Text
    {
        get => _text;
        set => SetText(value);
    }

    /// <summary>
    /// 设置单元格文本
    /// </summary>
    /// <param name="text"></param>
    public void SetText(string text)
    {
        _text = text;
        Body = new JObject
        {
            { "type", "tpl" },
            { "tpl", text },
            { "inline", false }
        };

        if (RowSpan > 1)
        {
            Body.Add("className", "row-Span-Label");  // 文字跨行样式
        }
        else
        {
            Body.Add("className", "extend-label"); // 不跨行单元格文字样式
        }
    }

    /// <summary>
    /// 设置文字对齐方式
    /// </summary>
    /// <param name="textAlign"></param>
    public void SetTextAlign(string textAlign)
    {
        switch (textAlign)
        {
            case "MiddleLeft":
                Style.Add("alignItems", "center");
                Style.Add("justifyContent", "flex-start");
                break;
            case "MiddleCenter":
                Style.Add("alignItems", "center");
                Style.Add("justifyContent", "center");
                break;
            case "MiddleRight":
                Style.Add("alignItems", "center");
                Style.Add("justifyContent", "flex-end");
                break;
            case "TopLeft":
                Style.Add("alignItems", "center");
                Style.Add("justifyContent", "flex-start");
                break;
            case "TopCenter":
                Style.Add("alignItems", "center");
                Style.Add("justifyContent", "center");
                break;
            case "TopRight":
                Style.Add("alignItems", "center");
                Style.Add("justifyContent", "flex-end");
                break;
            case "BottomLeft":
                Style.Add("alignItems", "center");
                Style.Add("justifyContent", "flex-start");
                break;
            case "BottomRight":
                Style.Add("alignItems", "center");
                Style.Add("justifyContent", "center");
                break;
            case "BottomCenter":
                Style.Add("alignItems", "center");
                Style.Add("justifyContent", "flex-end");
                break;
            default:
                Style.Add("alignItems", "center");
                Style.Add("justifyContent", "center");
                break;
        }
    }
}
